package com.qingxin.sys.utils;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.TexturePaint;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

/**
 * 图像工具包，该类提供图像处理的一些方法<br>
 * ImageIO支持读取BMP, GIF, JPG, JPEG, PNG和WBMP图像<br>
 * ImageIO支持写入BMP, GIF, JPG, JPEG, PNG和WBMP图像<br>
 * <br>
 * com.sun.imageio.plugins.bmp.BMPImageReader<br>
 * com.sun.imageio.plugins.gif.GIFImageReader<br>
 * com.sun.imageio.plugins.jpeg.JPEGImageReader<br>
 * com.sun.imageio.plugins.png.PNGImageReader<br>
 * com.sun.imageio.plugins.wbmp.WBMPImageReader<br>
 * <br>
 * com.sun.imageio.plugins.bmp.BMPImageWriter{CanWriteCompressed:true}<br>
 * com.sun.imageio.plugins.gif.GIFImageWriter{CanWriteCompressed:true}<br>
 * com.sun.imageio.plugins.jpeg.JPEGImageWriter{CanWriteCompressed:true}<br>
 * com.sun.imageio.plugins.png.PNGImageWriter{CanWriteCompressed:false}<br>
 * com.sun.imageio.plugins.wbmp.WBMPImageWriter{CanWriteCompressed:false}<br>
 * 
 * @author Administrator
 *
 */
public class ImageUtils {
	
	
	/**
	 * 创建缩略图
	 * @param imageFile
	 * @param width
	 * @param height
	 * @param thumb
	 * @return 缩略图文件对象
	 * @throws Exception
	 */
	public static File createThumbnail(File imageFile, int width, int height, String thumb) throws Exception {
		BufferedImage srcImage = readImage(imageFile);
		
		String fname = imageFile.getCanonicalPath();
		String ext = FilenameUtils.getExtension(fname).toLowerCase(Locale.ENGLISH);
		String thumbRealPath = fname.substring(0, fname.length()-ext.length()-1) + thumb + "." + ext;
		File thumbFile = new File(thumbRealPath);
		
		//图片较小时直接复制原图
		if(srcImage.getWidth() <= width && srcImage.getHeight()<=height){
			FileUtils.copyFile(imageFile, thumbFile);
			return thumbFile;
		}
		//创建缩小的图片，按等比缩放
		BufferedImage miniImage = getScaleImage(srcImage, width, height, true, 0);
		saveImage(miniImage, thumbFile, getImageTypeByExtension(ext), 1.0f);
		return thumbFile;
	}
	

	/**
	 * 获取缩放的图片
	 * @param img 原图
	 * @param scaleWidth 缩放后宽度
	 * @param scaleHeight 缩放后高度
	 * @param proportion 是否等比缩放
	 * @param keepType 参数proportion为true时有效：大于0时保持宽度，小于0时保持高度，等于0时宽高自适应
	 * @return 缩放后的图片
	 */
	public static BufferedImage getScaleImage(BufferedImage img, int scaleWidth, int scaleHeight, boolean proportion, int keepType){
		if(proportion){//等比缩放
			double rate = 1.0;
			if(keepType > 0){//保持宽度
				rate = img.getWidth()*1.0/scaleWidth;
			}else if(keepType < 0){//保持高度
				rate = img.getHeight()*1.0/scaleHeight;
			}else{//宽高自适应，使图像不溢出指定大小[scaleWidth*scaleHeight]
				double rw=0,rh=0;
				if(scaleWidth>0){
					rw = img.getWidth()*1.0/scaleWidth;
				}
				if(scaleHeight>0){
					rh = img.getHeight()*1.0/scaleHeight;
				}
				rate = Math.max(rw, rh);
			}
			scaleWidth = (int) (img.getWidth()/rate);
			scaleHeight = (int) (img.getHeight()/rate);
		}
		int type = img.getType();
		if(type==BufferedImage.TYPE_CUSTOM){
			type = BufferedImage.TYPE_INT_ARGB;
		}
		BufferedImage bimg = new BufferedImage(scaleWidth, scaleHeight, type);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH), 0, 0, null);
		return bimg;
	}

	
	/**
	 * 获取裁剪的图片，如果指定区域不包含在此 BufferedImage 中则抛出RasterFormatException
	 * @param img 原图
	 * @param subWidth 指定矩形区域的宽度
	 * @param subHeight 指定矩形区域的高度
	 * @param offsetX 指定矩形区域左上角的 X 坐标
	 * @param offsetY 指定矩形区域左上角的 Y 坐标
	 * @return 裁剪后的图像
	 */
	public static BufferedImage getSubimage(BufferedImage img, int subWidth, int subHeight, int offsetX, int offsetY){
		int type = img.getType();
		if(type==BufferedImage.TYPE_CUSTOM){
			type = BufferedImage.TYPE_INT_ARGB;
		}
		BufferedImage bimg = new BufferedImage(subWidth, subHeight, type);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img.getSubimage(offsetX, offsetY, subWidth, subHeight), null, 0, 0);
		return bimg;
	}

	
	/**
	 * 添加图片水印
	 * @param img 原图
	 * @param markimg 水印图片
	 * @param position 位置CENTER|TOP|BOTTOM|LEFT|RIGHT之一或组合
	 * @param offsetX 方向 X偏移
	 * @param offsetY 方向 Y偏移
	 * @param alpha 透明度[0.0f~1.0f]
	 * @param angle 旋转角度
	 * @return 带水印的图片
	 */
	public static BufferedImage printImageMark(BufferedImage img, BufferedImage markimg, int position, int offsetX, int offsetY, float alpha, int angle){
		if(alpha>1.0f || alpha<0.0f){
			throw new IllegalArgumentException("透明度必须为[0.0f~1.0f]之间的值.");
		}
		int type = img.getType();
		if(type==BufferedImage.TYPE_CUSTOM){
			type = BufferedImage.TYPE_INT_ARGB;
		}
		BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), type);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img, null, 0, 0);

		int px = 0, py = 0;
		if((position & LEFT)!=0){
			px = 0;
		}else if((position & RIGHT)!=0){
			px = img.getWidth()-markimg.getWidth();
		}else{
			px = (img.getWidth()-markimg.getWidth())/2;
		}
		if((position & TOP)!=0){
			py = 0;
		}else if((position & BOTTOM)!=0){
			py = img.getHeight()-markimg.getHeight();
		}else{
			py = (img.getHeight()-markimg.getHeight())/2;
		}
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));

		AffineTransform saveAT = g2d.getTransform();
		AffineTransform at = new AffineTransform();
		at.rotate(Math.toRadians(angle),px+offsetX+markimg.getWidth()/2,py+offsetY+markimg.getHeight()/2);
		g2d.transform(at);
		g2d.drawImage(markimg, null, px+offsetX, py+offsetY);
		g2d.setTransform(saveAT);
		return bimg;
	}
	
	
	/**
	 * 添加图片平铺水印
	 * @param img 原图
	 * @param markimg 水印图片
	 * @param spaceX 水印图片 X 方向间距
	 * @param spaceY 水印图片 Y 方向间距
	 * @param alpha 透明度[0.0f~1.0f]
	 * @param angle 旋转角度
	 * @return 带水印的图片
	 */
	public static BufferedImage printImageTextureMark(BufferedImage img, BufferedImage markimg, int spaceX, int spaceY, float alpha, int angle){
		if(alpha>1.0f || alpha<0.0f){
			throw new IllegalArgumentException("透明度必须为[0.0f~1.0f]之间的值.");
		}
		//创建纹理
		BufferedImage ti = new BufferedImage(markimg.getWidth()+spaceX, markimg.getHeight()+spaceY, BufferedImage.TYPE_INT_ARGB);
		Graphics2D gi = ti.createGraphics();
		gi.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		gi.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		gi.drawImage(markimg, null, spaceX/2, spaceY/2);
		TexturePaint texture = new TexturePaint(ti,new Rectangle(0,0,ti.getWidth(), ti.getHeight()));

		int type = img.getType();
		if(type==BufferedImage.TYPE_CUSTOM){
			type = BufferedImage.TYPE_INT_ARGB;
		}
		BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), type);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img, 0, 0, null);

		AffineTransform saveAT = g2d.getTransform();
		AffineTransform at = new AffineTransform();
		at.rotate(Math.toRadians(angle),img.getWidth()/2,img.getHeight()/2);
		g2d.transform(at);
		g2d.setPaint(texture);
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
		int r = img.getWidth()+img.getHeight();
		g2d.fillRect(-r, -r, 2*r, 2*r);
		g2d.setTransform(saveAT);

		return bimg;
	}

	
	/**
	 * 添加文字水印
	 * @param img 原图
	 * @param text 水印文字
	 * @param font 水印字体
	 * @param color 水印颜色
	 * @param position 位置CENTER|TOP|BOTTOM|LEFT|RIGHT之一或组合
	 * @param offsetX 方向 X 偏移
	 * @param offsetY 方向 Y 偏移
	 * @param alpha 透明度[0.0f~1.0f]
	 * @param angle 旋转角度
	 * @return 带水印的图片
	 */
	public static BufferedImage printTextMark(BufferedImage img, String text, Font font, Color color, int position, int offsetX, int offsetY, float alpha, int angle){
		if(alpha>1.0f || alpha<0.0f){
			throw new IllegalArgumentException("透明度必须为[0.0f~1.0f]之间的值.");
		}
		int type = img.getType();
		if(type==BufferedImage.TYPE_CUSTOM){
			type = BufferedImage.TYPE_INT_ARGB;
		}
		BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), type);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img, null, 0, 0);
		if(font!=null){
			g2d.setFont(font);
		}
		if(color!=null){
			g2d.setColor(color);
		}
		FontMetrics fm = g2d.getFontMetrics(g2d.getFont());
		int fw = fm.charsWidth(text.toCharArray(), 0, text.length());
		int px = 0, py = 0;
		if((position & LEFT)!=0){
			px = 0;
		}else if((position & RIGHT)!=0){
			px = img.getWidth()-fw;
		}else{
			px = (img.getWidth()-fw)/2;
		}
		if((position & TOP)!=0){
			py = fm.getAscent();
		}else if((position & BOTTOM)!=0){
			py = img.getHeight()-fm.getDescent();
		}else{
			py = img.getHeight()/2+(fm.getAscent()-fm.getDescent())/2;
		}
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));

		AffineTransform saveAT = g2d.getTransform();
		AffineTransform at = new AffineTransform();
		at.rotate(Math.toRadians(angle),px+offsetX+fw/2,py+offsetY-(fm.getAscent()-fm.getDescent())/2);
		g2d.transform(at);
		g2d.drawString(text, px+offsetX, py+offsetY);
		g2d.setTransform(saveAT);
		return bimg;
	}
	
	
	/**
	 * 添加文字平铺水印
	 * @param img 原图
	 * @param text 水印文字
	 * @param font 水印字体
	 * @param color 水印颜色
	 * @param spaceX 水印 X 方向间距
	 * @param spaceY 水印 Y 方向间距
	 * @param alpha 透明度[0.0f~1.0f]
	 * @param angle 旋转角度
	 * @return 带水印的图片
	 */
	public static BufferedImage printTextTextureMark(BufferedImage img, String text, Font font, Color color, int spaceX, int spaceY, float alpha, int angle){
		if(alpha>1.0f || alpha<0.0f){
			throw new IllegalArgumentException("透明度必须为[0.0f~1.0f]之间的值.");
		}
		int type = img.getType();
		if(type==BufferedImage.TYPE_CUSTOM){
			type = BufferedImage.TYPE_INT_ARGB;
		}
		BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), type);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img, 0, 0, null);
		if(font!=null){
			g2d.setFont(font);
		}
		FontMetrics fm = g2d.getFontMetrics(font);
		int fw = fm.charsWidth(text.toCharArray(), 0, text.length());
		int fh = fm.getAscent()+fm.getDescent();

		//创建纹理
		BufferedImage ti = new BufferedImage(fw+spaceX, fh+spaceY, BufferedImage.TYPE_INT_ARGB);
		Graphics2D gi = ti.createGraphics();
		gi.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		gi.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		if(font!=null){
			gi.setFont(font);
		}
		if(color!=null){
			gi.setColor(color);
		}
		gi.drawString(text, spaceX/2, spaceY/2+fm.getAscent());
		TexturePaint texture = new TexturePaint(ti,new Rectangle(0,0,ti.getWidth(), ti.getHeight()));

		AffineTransform saveAT = g2d.getTransform();
		AffineTransform at = new AffineTransform();
		at.rotate(Math.toRadians(angle),img.getWidth()/2,img.getHeight()/2);
		g2d.transform(at);
		g2d.setPaint(texture);
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
		int r = img.getWidth()+img.getHeight();
		g2d.fillRect(-r, -r, 2*r, 2*r);
		g2d.setTransform(saveAT);

		return bimg;
	}

	
	/**
	 * 获取透明图像
	 * @param img 原图
	 * @param alpha 透明度[0.0f~1.0f]
	 * @return 透明图像
	 */
	public static BufferedImage getAlphaImage(BufferedImage img, float alpha){
		if(alpha>1.0f || alpha<0.0f){
			throw new IllegalArgumentException("透明度必须为[0.0f~1.0f]之间的值.");
		}
		BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
		g2d.drawImage(img, 0, 0, null);
		return bimg;
	}

	
	/**
	 * 获取黑白图像（二值图像）
	 * @param img 原图
	 * @return 黑白图像
	 */
	public static BufferedImage getBinaryImage(BufferedImage img){
		if(img.getType()==BufferedImage.TYPE_BYTE_BINARY){
			return img;
		}
		BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img, 0, 0, null);
		return bimg;
	}

	
	/**
	 * 获取灰度图像
	 * @param img 原图
	 * @return 灰度图像
	 */
	public static BufferedImage getGrayImage(BufferedImage img){
		if(img.getType()==BufferedImage.TYPE_BYTE_GRAY){
			return img;
		}
		BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img, 0, 0, null);
		return bimg;
	}

	
	/**
	 * 获取索引图像（256色）
	 * @param img 原图
	 * @return 索引图像
	 */
	public static BufferedImage getIndexedImage(BufferedImage img){
		if(img.getType()==BufferedImage.TYPE_BYTE_INDEXED){
			return img;
		}
		BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img, 0, 0, null);
		return bimg;
	}

	
	/**
	 * 获取RGB图像
	 * @param img 原图
	 * @return RGB图像
	 */
	public static BufferedImage getRGBImage(BufferedImage img){
		if(img.getType()==BufferedImage.TYPE_INT_RGB){
			return img;
		}
		BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = bimg.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(img, 0, 0, null);
		return bimg;
	}

	
	/**
	 * 保存图片的方法，该方法不会关闭提供的ImageOutputStream，如果需要，调用者负责关闭该流。
	 * 
	 * @param img
	 *            要保存的图像对象
	 * @param output
	 *            要保存到的目标输出流
	 * @param imageType
	 *            图像类型
	 * @param quality
	 *            BMP、GIF和JPG格式支持压缩，该参数有效；PNG和WBMP格式不支持压缩，该参数无效。
	 *            图像质量[0.0f~1.0f]，1.0f为最高质量[高图像质量很重要]，0.0f为最低质量[高度压缩很重要]，ImageIO的默认值为0.75f
	 * @return 如果没有找到合适的 writer，则返回 false
	 * @throws Exception
	 */
	public static boolean saveImage(BufferedImage img, ImageOutputStream output, ImageType imageType, float quality) throws Exception {
		if(quality > 1.0f || quality < 0.0f){
			// 默认保存方法
			return ImageIO.write(img, imageType.getType().toUpperCase(), output);
		}
		ImageWriter iw = getImageWriter(imageType);
		if(iw!=null){
			ImageWriteParam iwp = iw.getDefaultWriteParam();
			if(iwp.canWriteCompressed()){
				iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);//设置该模式可以指定压缩参数
				iwp.setCompressionType(imageType.getCompressionType());
				iwp.setCompressionQuality(quality);
			}
			iw.setOutput(output);
			iw.write(null, new IIOImage(img, null, null), iwp);
			iw.dispose();
			output.flush();
			return true;
		}else{
			// 默认保存方法
			return ImageIO.write(img, imageType.getType().toUpperCase(), output);
		}
	}

	
	/**
	 * 保存图片的方法
	 * @param img
	 *            要保存的图像对象
	 * @param outfile
	 *            目标文件
	 * @param imageType
	 *            图像类型
	 * @param quality
	 *            图像质量[0.0f~1.0f]，只有当格式为BMP、GIF和JPG时该参数有效
	 * @return 如果没有找到合适的 writer，则返回 false
	 * @throws Exception
	 */
	public static boolean saveImage(BufferedImage img, File outfile, ImageType imageType, float quality) throws Exception {
		ImageOutputStream out = new FileImageOutputStream(outfile);
		boolean b = saveImage(img, out, imageType, quality);
		out.close();
		return b;
	}
	
	
	/**
	 * 从文件读取BufferedImage对象
	 * 
	 * @param imageFile
	 * @return
	 * @throws IOException
	 */
	public static BufferedImage readImage(File imageFile) throws IOException {
		return ImageIO.read(imageFile);
	}
	
	
	/**
	 * 从InputStream读取BufferedImage对象
	 * 
	 * @param imageInput
	 * @return
	 * @throws IOException
	 */
	public static BufferedImage readImage(InputStream imageInput) throws IOException {
		return ImageIO.read(imageInput);
	}
	
	
	/**
	 * 获取指定类型的图像编码器，如果没有找到则返回null
	 * 
	 * @param imageType
	 * @return
	 */
	private static ImageWriter getImageWriter(ImageType imageType){
		Class<? extends ImageWriter> cls = IMAGE_WRITERS.get(imageType);
		if(cls==null){
			return null;
		}
		ImageWriter imageWriter = null;
		Iterator<ImageWriter> iter = ImageIO.getImageWritersBySuffix(imageType.getType());
		while(iter.hasNext()){
			ImageWriter w = iter.next();
			if(cls.isInstance(w)){
				imageWriter = w;
				break;
			}
		}
		return imageWriter;
	}
	
	
	/**
	 * 图片格式接口
	 * @author JiWangli
	 *
	 */
	private interface ImageType {
		public String getType();
		public String getCompressionType();
	}

	
	/**
	 * 指定BMP图片格式
	 */
	public static final ImageType BMP = new ImageType() {
		@Override
		public String getType() {
			return "bmp";
		}
		@Override
		public String getCompressionType() {
			return "BI_RGB";
		}
	};
	

	/**
	 * 指定GIF图片格式
	 */
	public static final ImageType GIF = new ImageType() {
		@Override
		public String getType() {
			return "gif";
		}
		@Override
		public String getCompressionType() {
			return "LZW";
		}
	};

	
	/**
	 * 指定JPEG图片格式
	 */
	public static final ImageType JPEG = new ImageType() {
		@Override
		public String getType() {
			return "jpeg";
		}
		@Override
		public String getCompressionType() {
			return "JPEG";
		}
	};

	
	/**
	 * 指定PNG图片格式
	 */
	public static final ImageType PNG = new ImageType() {
		@Override
		public String getType() {
			return "png";
		}
		@Override
		public String getCompressionType() {
			return null;
		}
	};

	
	/**
	 * 指定WBMP图片格式
	 */
	public static final ImageType WBMP = new ImageType() {
		@Override
		public String getType() {
			return "wbmp";
		}
		@Override
		public String getCompressionType() {
			return null;
		}
	};
	
	
	/**
	 * 根据扩展名获取图片类型
	 * 
	 * @param extension
	 * @return
	 */
	public static ImageType getImageTypeByExtension(String extension){
		if("jpg".equalsIgnoreCase(extension) || "jpeg".equalsIgnoreCase(extension)){
			return JPEG;
		}
		if("png".equalsIgnoreCase(extension)){
			return PNG;
		}
		if("gif".equalsIgnoreCase(extension)){
			return GIF;
		}
		if("bmp".equalsIgnoreCase(extension)){
			return BMP;
		}
		if("wbmp".equalsIgnoreCase(extension)){
			return WBMP;
		}
		return null;
	}
	
	
	/**
	 * 图像编码类
	 */
	private static final Hashtable<ImageType, Class<? extends ImageWriter>> IMAGE_WRITERS;
	static{
		IMAGE_WRITERS = new Hashtable<ImageType, Class<? extends ImageWriter>>();
		IMAGE_WRITERS.put(BMP, com.sun.imageio.plugins.bmp.BMPImageWriter.class);
		IMAGE_WRITERS.put(GIF, com.sun.imageio.plugins.gif.GIFImageWriter.class);
		IMAGE_WRITERS.put(JPEG, com.sun.imageio.plugins.jpeg.JPEGImageWriter.class);
		IMAGE_WRITERS.put(PNG, com.sun.imageio.plugins.png.PNGImageWriter.class);
		IMAGE_WRITERS.put(WBMP, com.sun.imageio.plugins.wbmp.WBMPImageWriter.class);
	}
	

	/**
	 * 指定位置为居中
	 */
	public static final int CENTER = 0x0;

	/**
	 * 指定位置为TOP，优先级高于BOTTOM
	 */
	public static final int TOP = 0x1;

	/**
	 * 指定位置为BOTTOM，优先级低于TOP
	 */
	public static final int BOTTOM = 0x2;

	/**
	 * 指定位置为LEFT，优先级高于RIGHT
	 */
	public static final int LEFT = 0x4;

	/**
	 * 指定位置为RIGHT，优先级低于LEFT
	 */
	public static final int RIGHT = 0x8;
	
}
